package com.jinzhi.jzweb.utils;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.support.odps.udf.JSONArrayAdd;
import com.jinzhi.common.utils.JSONUtils;
import com.jinzhi.jzweb.domain.ExaminationCostDO;
import org.apache.velocity.util.ArrayListWrapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 考试费统计分析
 */
 public  class ExaminationCostAnalysisUtil {
     public static String[] province = { "江苏", "青海", "四川", "海南", "陕西", "甘肃", "云南", "湖南", "湖北", "黑龙江", "贵州", "山东","江西", "河南", "河北", "山西", "安徽", "福建", "浙江", "广东", "吉林", "辽宁", "台湾", "新疆", "广西", "宁夏", "内蒙古", "西藏", "北京", "天津","上海", "重庆", "香港", "澳门","南海诸岛"};

    public static Map<String,List<Map>> schoolMap(List<ExaminationCostDO> examinationCostDOS) throws JSONException {
        Map<String,List<Map>> mapList =  new HashMap<String,List<Map>>();
        List<Map> schoolNumList = new ArrayList<Map>();
        List<Map> examSchoolNumList = new ArrayList<Map>();
        List<Map> planNumList = new ArrayList<Map>();
        List<Map> stuNumList = new ArrayList<Map>();
        List<String> name=new ArrayList<String>();
        List<Integer> schoolNum=new ArrayList<Integer>();
        List<Integer> examSchoolNum = new ArrayList<Integer>();
        List<Integer> planStuNum=new ArrayList<Integer>();
        List<Integer> stuNum=new ArrayList<Integer>();
        for(ExaminationCostDO e : examinationCostDOS){
            String p = "";
            for(String pro : province){
                if(e.getProvince().contains(pro))
                {
                    p=pro;
                    break;
                }
            }
            int index=name.indexOf(p);
            if(index==-1){
                name.add(p);
                schoolNum.add(1);
                planStuNum.add(e.getPlanStuNum());
                if(e.getStuNum()==null){
                    stuNum.add(0);
                }else{
                    stuNum.add(e.getStuNum());
                }
                if(e.getState()>0)
                    examSchoolNum.add(1);
                else examSchoolNum.add(0);
            }else{
                schoolNum.set(index,schoolNum.get(index)+1);
                planStuNum.set(index,planStuNum.get(index)+e.getPlanStuNum());
                if(e.getStuNum()!=null){
                    stuNum.set(index,stuNum.get(index)+e.getStuNum());
                }
                if(e.getState()>0)
                    examSchoolNum.set(index,examSchoolNum.get(index)+1);
            }
        }
        for (int i=0;i<name.size();i++){
            Map map1 = new HashMap();
            map1.put("name",name.get(i));
            map1.put("value",schoolNum.get(i));
            Map map2 = new HashMap();
            map2.put("name",name.get(i));
            map2.put("value",planStuNum.get(i));
            Map map3 = new HashMap();
            map3.put("name",name.get(i));
            map3.put("value",stuNum.get(i));
            Map map4 = new HashMap();
            map4.put("name",name.get(i));
            map4.put("value",examSchoolNum.get(i));
            schoolNumList.add(map1);
            planNumList.add(map2);
            stuNumList.add(map3);
            examSchoolNumList.add(map4);
        }
        mapList.put("schoolNum",schoolNumList);
        mapList.put("examSchoolNum",examSchoolNumList);
        mapList.put("planStuNum",planNumList);
        mapList.put("stuNum",stuNumList);
        return mapList;
    }

    public static Map regionAnalysis(List<ExaminationCostDO> examinationCostDOS) throws JSONException {
        Map mapList =  new HashMap();
        List<String> name=new ArrayList<String>();
        List<Integer> schoolNum=new ArrayList<Integer>();
        List<Integer> planStuNum=new ArrayList<Integer>();
        List<Integer> stuNum=new ArrayList<Integer>();
        List<Integer> examSchoolNum = new ArrayList<Integer>();
        for(ExaminationCostDO e : examinationCostDOS){
            int index=name.indexOf(e.getRegionUserName());
            if(index==-1){
                name.add(e.getRegionUserName());
                schoolNum.add(1);
                planStuNum.add(e.getPlanStuNum());
                if(e.getStuNum()==null){
                    stuNum.add(0);
                }else{
                    stuNum.add(e.getStuNum());
                }
                if(e.getState()>0)
                    examSchoolNum.add(1);
                else examSchoolNum.add(0);
            }else{
                schoolNum.set(index,schoolNum.get(index)+1);
                planStuNum.set(index,planStuNum.get(index)+e.getPlanStuNum());
                if(e.getStuNum()!=null){
                    stuNum.set(index,stuNum.get(index)+e.getStuNum());
                }
                if(e.getState()>0)
                    examSchoolNum.set(index,examSchoolNum.get(index)+1);

            }
        }
        mapList.put("regionName",name);
        mapList.put("regionSchoolNum",schoolNum);
        mapList.put("regionPlanStuNum",planStuNum);
        mapList.put("regionStuNum",stuNum);
        mapList.put("regionExamSchoolNum",examSchoolNum);
        return mapList;
    }

    public static List<Map> statePie(List<ExaminationCostDO> examinationCostDOS) throws JSONException {
        List<Map> mapList = new ArrayList<Map>();
        Map map0=new HashMap();
        map0.put("name","未开始");
        map0.put("value",0);
        Map map1=new HashMap();
        map1.put("name","已考试");
        map1.put("value",0);
        Map map2=new HashMap();
        map2.put("name","已收款");
        map2.put("value",0);
        Map map3=new HashMap();
        map3.put("name","已返款（个人）");
        map3.put("value",0);
        Map map4=new HashMap();
        map4.put("name","已返款（学校）");
        map4.put("value",0);
        Map map5=new HashMap();
        map5.put("name","已完成");
        map5.put("value",0);
        for(ExaminationCostDO e : examinationCostDOS){
            switch (e.getState()){
                case 0:map0.put("value",(int)map0.get("value")+1);break;
                case 1:map1.put("value",(int)map1.get("value")+1);break;
                case 2:map2.put("value",(int)map2.get("value")+1);break;
                case 3:map3.put("value",(int)map3.get("value")+1);break;
                case 4:map4.put("value",(int)map4.get("value")+1);break;
                case 5:map5.put("value",(int)map5.get("value")+1);break;
            }
        }
        mapList.add(map0);
        mapList.add(map1);
        mapList.add(map2);
        mapList.add(map3);
        mapList.add(map4);
        mapList.add(map5);

        return mapList;
    }
}
